<?php

/**
 * @link http://tt.tryine.com/
 * @copyright Copyright (c) 2018 CSHOP
 * @author Lu Wei
 * Created by Adon.
 * User: Adon
 * Date: 2017/12/28
 * Time: 15:53
 */

namespace app\modules\user\controllers\mch;

use app\models\Goods;
use app\models\Mch;
//use app\models\MchGoods;
use app\models\GoodsPic;
use app\models\Cat;
use app\models\Card;
use app\models\GoodsCat;
use app\models\MchCat;
use app\models\MchGoodsCat;
use app\modules\mch\models\GoodsForm;
use app\models\PostageRules;
use app\models\OrderMessage;
use app\models\tplmsg\AdminTplMsgSender;
use app\modules\user\behaviors\MchBehavior;
use app\modules\user\behaviors\UserLoginBehavior;
use app\modules\user\controllers\Controller;
use app\modules\user\models\mch\CatEditForm;
use app\modules\user\models\mch\CatListForm;
use app\modules\user\models\mch\GoodsEditForm;
use yii\data\Pagination;

class GoodsController extends Controller
{

    public function behaviors()
    {
        return [
            'login' => [
                'class' => UserLoginBehavior::className(),
            ],
            'mch' => [
                'class' => MchBehavior::className(),
            ],
        ];
    }

    public function actionIndex($keyword = null, $status = null)
    {


        $query_cat = GoodsCat::find()->alias('gc')->leftJoin(['c' => Cat::tableName()], 'c.id=gc.cat_id')
            ->where(['gc.store_id' => $this->store->id, 'gc.is_delete' => 0])->select('gc.goods_id,c.name,gc.cat_id');
        $query = Goods::find()->alias('g')->where(['g.is_delete' => 0, 'g.mch_id' => $this->mch->id]);

        if ($status != null) {
            $query->andWhere('g.status=:status', [':status' => $status]);
        }
        $query->leftJoin(['c' => Cat::tableName()], 'c.id=g.cat_id');
        $query->leftJoin(['gc' => $query_cat], 'gc.goods_id=g.id');
        //$query->leftJoin(['mg' => MchGoods::tableName()], 'mg.goods_id=g.id');

        $cat_query = clone $query;

        $query->select('g.id,g.name,g.price,g.original_price,g.status,g.cover_pic,g.sort,g.goods_num,g.attr,g.cat_id,g.virtual_sales,g.store_id,g.quick_purchase');
        if (trim($keyword)) {
            $query->andWhere(['LIKE', 'g.name', $keyword]);
        }
        if (isset($_GET['cat'])) {
            $cat = trim($_GET['cat']);
            $query->andWhere([
                'or',
                ['c.name' => $cat],
                ['gc.name' => $cat],
            ]);
        }
        $cat_list = $cat_query->groupBy('name')->orderBy(['g.cat_id' => SORT_ASC])->select([
            '(case when g.cat_id=0 then gc.name else c.name end) name',
        ])->asArray()->column();
        $query->groupBy('g.id');
        $count = $query->count();
        $pagination = new Pagination(['totalCount' => $count]);
        $list = $query->orderBy('g.sort ASC,g.addtime DESC')
            ->limit($pagination->limit)
            ->offset($pagination->offset)
            ->with(['goodsPicList', 'catList1.cat', 'cat'])
            ->all();

//        foreach ($list as $k=>$v ) {
//            $mchGoods = Goods::findOne(['goods_id'=>$v->id,'mch_id'=>$this->mch->id]);
//            $v['status'] = 0;
//            if($mchGoods)   $v['status'] = 1;
//            $list[$k] = $v ;
//        }
        return $this->render('index', [
            'list' => $list,
            'pagination' => $pagination,
            'cat_list' => $cat_list,
        ]);

//        $keyword = trim($keyword);
//        $query = Goods::find()->where(['mch_id' => $this->mch->id, 'is_delete' => 0]);
//        if ($keyword) {
//            $query->andWhere(['LIKE', 'name', $keyword]);
//        }
//        if ($cat_id) {
//            $sub_query = MchGoodsCat::find()->select('goods_id')->where(['cat_id' => $cat_id]);
//            $query->andWhere(['id' => $sub_query]);
//        }
//        $count = $query->count();
//        $pagination = new Pagination(['totalCount' => $count]);
//        $list = $query->select('id,name,cover_pic,status,attr,mch_sort')
//            ->orderBy('mch_sort,addtime DESC')->limit($pagination->limit)->offset($pagination->offset)->all();
//        return $this->render('index', [
//            'list' => $list,
//            'pagination' => $pagination,
//            'keyword' => $keyword,
//            'cat_list' => MchCat::find()->where(['mch_id' => $this->mch->id, 'is_delete' => 0, 'parent_id' => 0])->orderBy('sort,addtime DESC')->all(),
//            'get' => \Yii::$app->request->get(),
//        ]);
    }


    /**
     * 商品管理
     * @return string
     */
    public function actionGoods($keyword = null, $status = null)
    {
        $query_cat = GoodsCat::find()->alias('gc')->leftJoin(['c' => Cat::tableName()], 'c.id=gc.cat_id')
            ->where(['gc.store_id' => $this->store->id, 'gc.is_delete' => 0])->select('gc.goods_id,c.name,gc.cat_id');
        $query = Goods::find()->alias('g')->where(['g.store_id' => $this->store->id, 'g.is_delete' => 0, 'g.mch_id' => 0,'status'=>1]);

        if ($status != null) {
            $query->andWhere('g.status=:status', [':status' => $status]);
        }
        $query->leftJoin(['c' => Cat::tableName()], 'c.id=g.cat_id');
        $query->leftJoin(['gc' => $query_cat], 'gc.goods_id=g.id');
        //$query->leftJoin(['mg' => MchGoods::tableName()], 'mg.goods_id=g.id');

        $cat_query = clone $query;

        $query->select('g.id,g.name,g.price,g.original_price,g.status,g.cover_pic,g.sort,g.attr,g.cat_id,g.virtual_sales,g.store_id,g.quick_purchase');
        if (trim($keyword)) {
            $query->andWhere(['LIKE', 'g.name', $keyword]);
        }
        if (isset($_GET['cat'])) {
            $cat = trim($_GET['cat']);
            $query->andWhere([
                'or',
                ['c.name' => $cat],
                ['gc.name' => $cat],
            ]);
        }
        $cat_list = $cat_query->groupBy('name')->orderBy(['g.cat_id' => SORT_ASC])->select([
            '(case when g.cat_id=0 then gc.name else c.name end) name',
        ])->asArray()->column();
        $query->groupBy('g.id');
        $count = $query->count();
        $pagination = new Pagination(['totalCount' => $count]);
        $list = $query->orderBy('g.sort ASC,g.addtime DESC')
            ->limit($pagination->limit)
            ->offset($pagination->offset)
            ->with(['goodsPicList', 'catList1.cat', 'cat'])
            ->all();

        foreach ($list as $k=>$v ) {
            $mchGoods = Goods::findOne(['goods_id'=>$v->id,'mch_id'=>$this->mch->id]);
            $v['status'] = 0;
            if($mchGoods)   $v['status'] = 1;
            $list[$k] = $v ;
        }
        return $this->render('goods', [
            'list' => $list,
            'pagination' => $pagination,
            'cat_list' => $cat_list,
        ]);
    }



    /**
     * 商品管理
     * @return string
     */
    public function actionGoods1($keyword = null, $status = null)
    {
        $query_cat = GoodsCat::find()->alias('gc')->leftJoin(['c' => Cat::tableName()], 'c.id=gc.cat_id')
            ->where(['gc.store_id' => $this->store->id, 'gc.is_delete' => 0])->select('gc.goods_id,c.name,gc.cat_id');
        $query = Goods::find()->alias('g')->where(['g.store_id' => $this->store->id, 'g.is_delete' => 0, 'g.mch_id' => $this->mch->id,'g.goods_id'=>0]);

        if ($status != null) {
            $query->andWhere('g.status=:status', [':status' => $status]);
        }
        $query->leftJoin(['c' => Cat::tableName()], 'c.id=g.cat_id');
        $query->leftJoin(['gc' => $query_cat], 'gc.goods_id=g.id');
        //$query->leftJoin(['mg' => MchGoods::tableName()], 'mg.goods_id=g.id');

        $cat_query = clone $query;

        $query->select('g.id,g.name,g.price,g.original_price,g.audit_status,g.cover_pic,g.sort,g.attr,g.cat_id,g.virtual_sales,g.store_id,g.quick_purchase');
        if (trim($keyword)) {
            $query->andWhere(['LIKE', 'g.name', $keyword]);
        }
        if (isset($_GET['cat'])) {
            $cat = trim($_GET['cat']);
            $query->andWhere([
                'or',
                ['c.name' => $cat],
                ['gc.name' => $cat],
            ]);
        }
        $cat_list = $cat_query->groupBy('name')->orderBy(['g.cat_id' => SORT_ASC])->select([
            '(case when g.cat_id=0 then gc.name else c.name end) name',
        ])->asArray()->column();
        $query->groupBy('g.id');
        $count = $query->count();
        $pagination = new Pagination(['totalCount' => $count]);
        $list = $query->orderBy('g.sort ASC,g.addtime DESC')
            ->limit($pagination->limit)
            ->offset($pagination->offset)
            ->with(['goodsPicList', 'catList1.cat', 'cat'])
            ->all();

//        foreach ($list as $k=>$v ) {
//            $mchGoods = Goods::findOne(['goods_id'=>$v->id,'mch_id'=>$this->mch->id]);
//            $v['status'] = 0;
//            if($mchGoods)   $v['status'] = 1;
//            $list[$k] = $v ;
//        }
        return $this->render('goods1', [
            'list' => $list,
            'pagination' => $pagination,
            'cat_list' => $cat_list,
        ]);
    }



    //商品上下架
    public function actionGoodsUpDown($id = 0, $type = 'down')
    {
        if ($type == 'down') {
            $goods = Goods::findOne(['id' => $id, 'is_delete' => 0, 'store_id' => $this->store->id]);
            if (!$goods) {
                return [
                    'code' => 1,
                    'msg' => '商品已删除或已下架',
                ];
            }
            // $goods->status = 0;
        } elseif ($type == 'up') {
            $goods = Goods::findOne(['id' => $id, 'is_delete' => 0,'store_id' => $this->store->id]);

            if (!$goods) {
                return [
                    'code' => 1,
                    'msg' => '商品已删除或已上架',
                ];
            }

//            if (!$goods->getNum()) {
//                $return_url = \Yii::$app->urlManager->createUrl(['mch/goods/goods-edit', 'id' => $goods->id]);
//                if (!$goods->use_attr) {
//                    $return_url = \Yii::$app->urlManager->createUrl(['mch/goods/goods-edit', 'id' => $goods->id]) . '#step3';
//                }
//
//                return [
//                    'code' => 1,
//                    'msg' => '商品库存不足，请先完善商品库存',
//                    'return_url' => $return_url,
//                ];
//            }
            // $goods->status = 1;


            //增加产品
            $this->addMchGoods($goods);



        } elseif ($type == 'start') {
            $goods = Goods::findOne(['id' => $id, 'is_delete' => 0, 'store_id' => $this->store->id]);

            if (!$goods) {
                return [
                    'code' => 1,
                    'msg' => '商品已删除或已加入',
                ];
            }
            $goods->quick_purchase = 1;
        } elseif ($type == 'close') {
            $goods = Goods::findOne(['id' => $id, 'is_delete' => 0, 'store_id' => $this->store->id]);

            if (!$goods) {
                return [
                    'code' => 1,
                    'msg' => '商品已删除或已关闭',
                ];
            }
            $goods->quick_purchase = 0;
        } else {
            return [
                'code' => 1,
                'msg' => '参数错误',
            ];
        }
        if ($goods->save()) {
            return [
                'code' => 0,
                'msg' => '成功',
            ];
        } else {
            foreach ($goods->errors as $errors) {
                return [
                    'code' => 1,
                    'msg' => $errors[0],
                ];
            }
        }
    }


    public function addMchGoods($goods)
    {
        //站点管理
        $mch_goods = new Goods();
        $mch_goods->goods_id = $goods->id;
        $mch_goods->mch_id = $this->mch->id;
        $mch_goods->price = $goods->price;
        $mch_goods->status = 1;
        $mch_goods->store_id = $goods->store_id;
        $mch_goods->name = $goods->name;
        $mch_goods->original_price = $goods->original_price;
        $mch_goods->detail = $goods->detail;
        $mch_goods->addtime = time();
        $mch_goods->is_delete = $goods->is_delete;
        $mch_goods->attr = $goods->attr;
        $mch_goods->service = $goods->service;
        $mch_goods->sort = $goods->sort;
        $mch_goods->virtual_sales = $goods->virtual_sales;
        $mch_goods->cover_pic = $goods->cover_pic;
        $mch_goods->video_url = $goods->video_url;
        $mch_goods->unit = $goods->unit;
        $mch_goods->individual_share = $goods->individual_share;
        $mch_goods->share_commission_first = $goods->share_commission_first;
        $mch_goods->share_commission_second = $goods->share_commission_second;
        $mch_goods->share_commission_third = $goods->share_commission_third;
        $mch_goods->weight = $goods->weight;
        $mch_goods->freight = $goods->freight;
        $mch_goods->full_cut = $goods->full_cut;
        $mch_goods->use_attr = $goods->use_attr;
        $mch_goods->share_type = $goods->share_type;
        $mch_goods->goods_num = $goods->goods_num;
        $mch_goods->mch_sort = $goods->mch_sort;
        $mch_goods->save();

        //图片
        $goodsPic = GoodsPic::find()->where(['goods_id'=>$goods->id,'is_delete'=>0])->all();
        foreach ( $goodsPic as $v) {
            $goodsPicModel = new GoodsPic();
            $goodsPicModel->goods_id = $mch_goods->id;
            $goodsPicModel->pic_url = $v->pic_url;
            $goodsPicModel->save();
        }

        //分类
        $goodsCat = GoodsCat::find()->where(['goods_id'=>$goods->id,'is_delete'=>0])->all();
        foreach ( $goodsCat as $v) {
            $goodsCatModel = new GoodsCat();
            $goodsCatModel->goods_id = $mch_goods->id;
            $goodsCatModel->store_id = $this->store->id;
            $goodsCatModel->cat_id = $v->cat_id;
            $goodsCatModel->is_delete = 0;
            $goodsCatModel->addtime = time();
            $goodsCatModel->save();
        }
    }


    public function actionEdit($id = null)
    {
        $model = Goods::findOne(['id' => $id, 'mch_id' => $this->mch->id, 'is_delete' => 0]);
        if (!$model) {
            $model = new Goods();
            $model->mch_id = $this->mch->id;
            $model->store_id = $this->mch->store_id;
        }
        $form = new GoodsEditForm();
        $form->attributes = \Yii::$app->request->post();
        $form->model = $model;
        if (\Yii::$app->request->isPost) {
            return $form->save();
        } else {
            if (\Yii::$app->request->isAjax) {
                return $form->search();
            } else {
                $cat_list_form = new CatListForm();
                $cat_list_form->mch_id = $this->mch->id;
                return $this->render('edit');
            }
        }
    }


    /**
     * 商品修改
     * @param int $id
     * @return string
     */
    public function actionGoodsEdit($id = 0)
    {

        $goods = Goods::findOne(['id' => $id, 'store_id' => $this->store->id, 'mch_id' => $this->mch->id]);

        if (!$goods) {
            $goods = new Goods();
        }else{
            $goods->pick_time= date('Y-m-d H:i:s');
            $goods->arrival_time=date('Y-m-d H:i:s');
        }
        $form = new GoodsForm();
        if (\Yii::$app->request->isPost) {
            $model = \Yii::$app->request->post('model');
            if ($model['quick_purchase'] == 0) {
                $model['hot_cakes'] = 0;
            }

            $model['store_id'] = $this->store->id;
            $model['mch_id'] = $this->mch->id;
            $form->attributes = $model;

            $form->attr = \Yii::$app->request->post('attr');
            $form->goods_card = \Yii::$app->request->post('goods_card');
            $form->full_cut = \Yii::$app->request->post('full_cut');
            $form->integral = \Yii::$app->request->post('integral');
            $form->pick_time = date('Y-m-d H:i:s');
            $form->arrival_time = date('Y-m-d H:i:s');
            $form->goods = $goods;
            return $form->save();
        }

        $cat_list = Cat::find()->where(['store_id' => $this->store->id, 'is_delete' => 0, 'parent_id' => 0])->all();
        $postageRiles = PostageRules::find()->where(['store_id' => $this->store->id, 'is_delete' => 0])->all();
        $card_list = Card::find()->where(['store_id' => $this->store->id, 'is_delete' => 0])->asArray()->all();
        if ($goods->full_cut) {
            $goods->full_cut = json_decode($goods->full_cut, true);
        } else {
            $goods->full_cut = [
                'pieces' => '',
                'forehead' => '',
            ];
        }
        if ($goods->integral) {
            $goods->integral = json_decode($goods->integral, true);
        } else {
            $goods->integral = [
                'give' => 0,
                'deduction' => 0,
                'more' => 0,
            ];
        }
        $goods_card_list = Goods::getGoodsCard($goods->id);
        $goods_cat_list = Goods::getCatList($goods);
        foreach ($goods as $index => $value) {
            if (in_array($index, ['attr', 'full_cat', 'integral', 'payment', 'detail'])) {
                continue;
            }
            if (is_array($value) || is_object($value)) {
                continue;
            }
            $goods[$index] = str_replace("\"", "&quot;", $value);
        }


        return $this->render('goods-edit', [
            'goods' => $goods,
            'cat_list' => $cat_list,
            'postageRiles' => $postageRiles,
            'card_list' => \Yii::$app->serializer->encode($card_list),
            'goods_card_list' => \Yii::$app->serializer->encode($goods_card_list),
            'goods_cat_list' => \Yii::$app->serializer->encode($goods_cat_list),
        ]);
    }



    /**
     * 商品修改
     * @param int $id
     * @return string
     */
    public function actionGoodsEdit1($id = 0)
    {

        $goods = Goods::findOne(['id' => $id, 'store_id' => $this->store->id, 'mch_id' => $this->mch->id]);

        if (!$goods) {
            $goods = new Goods();
        }else{
            $goods->mch_id= $this->mch->id;
            $goods->is_sq= 1;
            $goods->pick_time= date('Y-m-d H:i:s');
            $goods->arrival_time=date('Y-m-d H:i:s');
        }
        $form = new GoodsForm();
        if (\Yii::$app->request->isPost) {
            $model = \Yii::$app->request->post('model');
            if ($model['quick_purchase'] == 0) {
                $model['hot_cakes'] = 0;
            }

            $model['store_id'] = $this->store->id;
            $form->attributes = $model;

            $form->attr = \Yii::$app->request->post('attr');
            $form->goods_card = \Yii::$app->request->post('goods_card');
            $form->full_cut = \Yii::$app->request->post('full_cut');
            $form->integral = \Yii::$app->request->post('integral');
            $form->mch_id =  $this->mch->id;
            $form->is_sq =  1;
            $form->pick_time = date('Y-m-d H:i:s');
            $form->arrival_time = date('Y-m-d H:i:s');
            $form->goods = $goods;
            return $form->save();
        }

        $cat_list = Cat::find()->where(['store_id' => $this->store->id, 'is_delete' => 0, 'parent_id' => 0])->all();
        $postageRiles = PostageRules::find()->where(['store_id' => $this->store->id, 'is_delete' => 0])->all();
        $card_list = Card::find()->where(['store_id' => $this->store->id, 'is_delete' => 0])->asArray()->all();
        if ($goods->full_cut) {
            $goods->full_cut = json_decode($goods->full_cut, true);
        } else {
            $goods->full_cut = [
                'pieces' => '',
                'forehead' => '',
            ];
        }
        if ($goods->integral) {
            $goods->integral = json_decode($goods->integral, true);
        } else {
            $goods->integral = [
                'give' => 0,
                'deduction' => 0,
                'more' => 0,
            ];
        }
        $goods_card_list = Goods::getGoodsCard($goods->id);
        $goods_cat_list = Goods::getCatList($goods);
        foreach ($goods as $index => $value) {
            if (in_array($index, ['attr', 'full_cat', 'integral', 'payment', 'detail'])) {
                continue;
            }
            if (is_array($value) || is_object($value)) {
                continue;
            }
            $goods[$index] = str_replace("\"", "&quot;", $value);
        }


        return $this->render('goods-edit1', [
            'goods' => $goods,
            'cat_list' => $cat_list,
            'postageRiles' => $postageRiles,
            'card_list' => \Yii::$app->serializer->encode($card_list),
            'goods_card_list' => \Yii::$app->serializer->encode($goods_card_list),
            'goods_cat_list' => \Yii::$app->serializer->encode($goods_cat_list),
        ]);
    }


    /**
     * 商品删除
     */
    public function actionDelete($id)
    {
        $model = Goods::findOne([
            'id' => $id,
            'mch_id' => $this->mch->id,
        ]);
        if (!$model) {
            return [
                'code' => 1,
                'msg' => '商品不存在',
            ];
        }
        $model->is_delete = 1;
        if ($model->save()) {
            return [
                'code' => 0,
                'msg' => '删除成功',
            ];
        } else {
            return [
                'code' => 1,
                'msg' => '删除失败',
            ];
        }
    }

    /**
     * 分类列表
     */
    public function actionCat()
    {
        $cat_list = MchCat::find()->where(['mch_id' => $this->mch->id, 'is_delete' => 0, 'parent_id' => 0])->orderBy('sort,addtime DESC')->all();
        return $this->render('cat', [
            'cat_list' => $cat_list,
        ]);
    }

    /**
     * 分类编辑
     */
    public function actionCatEdit($id = null)
    {
        $cat = MchCat::findOne(['id' => $id, 'mch_id' => $this->mch->id, 'is_delete' => 0]);
        if (!$cat) {
            $cat = new MchCat();
            $cat->mch_id = $this->mch->id;
        }
        if (\Yii::$app->request->isPost) {
            $form = new CatEditForm();
            $form->attributes = \Yii::$app->request->post('model');
            $form->model = $cat;
            return $form->save();
        }
        $parent_list_query = MchCat::find()->where([
            'mch_id' => $this->mch->id,
            'is_delete' => 0,
            'parent_id' => 0,
        ]);
        if (!$cat->isNewRecord && $cat->parent_id == 0) {
            $parent_list_query->andWhere([
                'id' => -1,
            ]);
        }
        $parent_list = $parent_list_query->all();
        return $this->render('cat-edit', [
            'parent_list' => $parent_list,
            'list' => $cat,
        ]);
    }

    /**
     * 分类删除
     */
    public function actionCatDel($id)
    {
        $model = MchCat::findOne([
            'id' => $id,
            'mch_id' => $this->mch->id,
        ]);
        if (!$model) {
            return [
                'code' => 1,
                'msg' => '分类不存在',
            ];
        }
        $model->is_delete = 1;
        if ($model->save()) {
            return [
                'code' => 0,
                'msg' => '删除成功',
            ];
        } else {
            return [
                'code' => 1,
                'msg' => '删除失败',
            ];
        }
    }

    /**
     * 改变商品上下架状态
     */
    public function actionSetStatus($id, $status)
    {
        $model = Goods::findOne([
            'id' => $id,
            'mch_id' => $this->mch->id,
        ]);
        if (!$model) {
            return [
                'code' => 1,
                'msg' => '商品不存在',
            ];
        }
//        if ($status != 0) {
//            return [
//                'code' => 1,
//                'msg' => '系统错误，请刷新重试',
//            ];
//        }
        $model->status = $status == 1 ? 1 : 0;
        if ($model->status == 1 && !$model->getNum()) {
            return [
                'code' => 1,
                'msg' => '商品库存为0上架失败，请先设置商品库存',
            ];
        }
        if ($model->save()) {
            return [
                'code' => 0,
                'msg' => $model->status == 1 ? '上架成功' : '下架成功',
            ];
        } else {
            return [
                'code' => 0,
                'msg' => $status == 1 ? '上架失败' : '下架失败',
            ];
        }
    }

    public function actionUpdateGoodsNum($offset)
    {
        /** @var Goods[] $list */
        $list = Goods::find()->select('id,attr,goods_num')->where(['mch_id' => $this->mch->id, 'is_delete' => 0])
            ->offset($offset)->limit(10)->all();
        foreach ($list as $item) {
            $item->updateAttributes([
                'goods_num' => $item->getNum(),
            ]);
        }
        if (!is_array($list) || !count($list)) {
            return [
                'code' => 0,
                'msg' => '更新完成',
            ];
        } else {
            return [
                'code' => 0,
                'msg' => 'success',
                'continue' => 1,
            ];
        }
    }

    public function actionApply($id)
    {
        $model = Goods::findOne([
            'id' => $id,
            'mch_id' => $this->mch->id,
            'store_id' => $this->store->id,
        ]);
        if (!$model) {
            return [
                'code' => 1,
                'msg' => '商品不存在',
            ];
        }
//        OrderMessage::set($model->id, $model->store_id, 4, 1);
//        AdminTplMsgSender::sendMchUploadGoods($this->store->id, [
//            'goods' => $model->name,
//        ]);
        $model->status = 1;
        $model->save();
        return [
            'code' => 0,
            'msg' => '提交成功',
        ];
    }

    /**
     * 批量设置
     */
    public function actionBatch()
    {
        $get = \Yii::$app->request->get();
        $res = 0;
        $goods_group = $get['goods_group'];
        $goods_id_group = [];
        foreach ($goods_group as $index => $value) {
            if ($get['type'] == 0) {
                if ($value['num'] != 0) {
                    array_push($goods_id_group, $value['id']);
                }
            } else {
                array_push($goods_id_group, $value['id']);
            }
        }

        if ($get['type'] == 0) { //批量上架
            foreach ($goods_id_group as $goods_id) {
                $mch_goods = Goods::findOne(['goods_id' => $goods_id, 'mch_id' => $this->mch->id]);
                if($mch_goods) continue;

                $goods = Goods::findOne(['id' => $goods_id, 'is_delete' => 0,'store_id' => $this->store->id]);
                if(!$goods) continue;

                $this->addMchGoods($goods);
            }

        }

        return ['code' => 0, 'msg' => '设置成功'];
    }

}
